/*
 * Created on Nov 27, 2005
 */
package dbsucxentW.pathProcessorW;

import dbsucxentW.constantW.Constant;
import java.util.regex.*;

/**
 * keeps track of information of a step
 * @author Seah Boon Siew modified by Klarinda
 */
public class Step {
	String _axis, _nameTest;

	String[] _predicate, _equality, _equalityOperator;

	boolean _hasPredicate;

	boolean[] _hasEquality;

	int typeId;

	int[] _toPred, _fromPred;

	// klarinda 20061122
	int[] _noOfOpenParenthesis;

	int[] _noOfCloseParenthesis;

	boolean[] _andExpr;

	boolean[] _orExpr;

	// end of klarinda

	/*
	 * public static int CHILD = 0, FOLLOWING = 1, PRECEDING = 2,
	 * FOLLOWING_SIBLING = 3, PRECEDING_SIBLING = 4; public static int ATTRIBUTE =
	 * 5, PARENT = 6, SELF = 7; public static int ANCESTOR = 8, ANCESTOR_OR_SELF =
	 * 9, DESCENDANT = 10, DESCENDANT_OR_SELF = 11;
	 */

	public Step(String axis, String nameTest, String[] predicate,
			String[] equality, boolean hasPredicate, boolean[] hasEquality,
			String[] equalityOperator, int[] openPar, int[] closePar,
			boolean[] and, boolean or[]) { // added by klarinda 20061122

		_axis = axis;
		_nameTest = nameTest;
		_predicate = predicate;
		_equality = equality;
		_hasPredicate = hasPredicate;
		_hasEquality = hasEquality;
		_equalityOperator = equalityOperator;

		// klarinda 20061122
		_noOfOpenParenthesis = openPar;
		_noOfCloseParenthesis = closePar;
		_andExpr = and;
		_orExpr = or;
		// end of klarinda

		if (axis.compareTo("child") == 0) {
			typeId = Constant.CHILD;
		} else if (axis.compareTo("following") == 0) {
			typeId = Constant.FOLLOWING;
		} else if (axis.compareTo("preceding") == 0) {
			typeId = Constant.PRECEDING;
		} else if (axis.compareTo("following-sibling") == 0) {
			typeId = Constant.FOLLOWING_SIBLING;
		} else if (axis.compareTo("preceding-sibling") == 0) {
			typeId = Constant.PRECEDING_SIBLING;
		} else if (axis.compareTo("attribute") == 0) {
			// klarinda 20061108 typeId = Constant.CHILD;
			// klarinda 20061108 _nameTest = "@" + _nameTest;
			typeId = Constant.ATTRIBUTE; // klarinda 20061108
		} else if (axis.compareTo("parent") == 0) {
			typeId = Constant.PARENT;
		} else if (axis.compareTo("self") == 0) {
			typeId = Constant.SELF;
		} else if (axis.compareTo("ancestor") == 0) {
			typeId = Constant.ANCESTOR;
		} else if (axis.compareTo("ancestor-or-self") == 0) {
			typeId = Constant.ANCESTOR_OR_SELF;
		} else if (axis.compareTo("descendant") == 0) {
			typeId = Constant.DESCENDANT;
		} else if (axis.compareTo("descendant-or-self") == 0) {
			typeId = Constant.DESCENDANT_OR_SELF;
		}

		_fromPred = new int[predicate.length];
		_toPred = new int[predicate.length];

		// split TO, if any
		/*
		 */
		/*
		 * for (int i = 0; i < predicate.length; i++) if (_hasEquality[i] ==
		 * false && _hasPredicate == true){
		 * 
		 * _predicate[i] = _predicate[i].toUpperCase();
		 * 
		 * String[] toParts = _predicate[i].split("TO"); if (toParts.length >
		 * 1){ Integer castedInt = new Integer(toParts[0].trim()); _fromPred[i] =
		 * castedInt.intValue(); castedInt = new Integer(toParts[1].trim());
		 * _toPred[i] = castedInt.intValue(); }else{ Integer castedInt = new
		 * Integer(toParts[0].trim()); _fromPred[i] = castedInt.intValue();
		 * _toPred[i] = _fromPred[i]; } }
		 */
		// Added by Erwin
		for (int i = 0; i < predicate.length; i++) {
			// testing : position()=x to y
			if (_hasEquality[i] == false && _hasPredicate == true) {
				Pattern pat = Pattern.compile("([0-9])+ (TO) ([0-9])+");
				Matcher mat = pat.matcher(predicate[i].toUpperCase());
				String[] toParts = _predicate[i].toUpperCase().split("TO");
				if (mat.matches()) {
					Integer castedInt = new Integer(toParts[0].trim());
					_fromPred[i] = castedInt.intValue();
					castedInt = new Integer(toParts[1].trim());
					_toPred[i] = castedInt.intValue();

				} else if (_equalityOperator[i].equals("<")) {
					if (predicate[i].startsWith("last()")) {
						_fromPred[i] = -1;
						_toPred[i] = -1;
					} else {
						Integer castedInt = new Integer(toParts[0].trim());
						_fromPred[i] = 0;
						_toPred[i] = castedInt - 1;
					}
				} else if (_equalityOperator[i].equals("<=")) {
					Integer castedInt = new Integer(toParts[0].trim());
					_fromPred[i] = 0;
					_toPred[i] = castedInt;
				} else if (_equalityOperator[i].equals(">=")) {
					Integer castedInt = new Integer(toParts[0].trim());
					_fromPred[i] = castedInt - 1;
					_toPred[i] = -1;
				} else if (_equalityOperator[i].equals(">")) {
					Integer castedInt = new Integer(toParts[0].trim());
					_fromPred[i] = castedInt;
					_toPred[i] = -1;
				} else {
					if (predicate[i].startsWith("last()")) {
						_fromPred[i] = -1;
						_toPred[i] = -1;
					} else {
						Integer castedInt = new Integer(toParts[0].trim());
						_fromPred[i] = castedInt.intValue();
						_toPred[i] = _fromPred[i];
					}
				}
			}
		}

	}

	public String[] getEqualityOperator() {
		return _equalityOperator;
	}

	public String getEqualityOperator(int i) {
		return _equalityOperator[i];
	}

	public void setEquality(int i, String value) {
		_equality[i] = value;
	}
	
	public String getAxis() {
		return _axis;
	}

	public String getNameTest() {
		return _nameTest;
	}

	public String[] getPredicate() {
		return _predicate;
	}

	public String getPredicate(int i) {
		return _predicate[i];
	}

	public String[] getEquality() {
		return _equality;
	}

	public String getEquality(int i) {
		return _equality[i];
	}

	public int[] getPredicateFrom() {
		return _fromPred;
	}

	public int getPredicateFrom(int i) {
		return _fromPred[i];
	}

	public int[] getPredicateTo() {
		return _toPred;
	}

	public int getPredicateTo(int i) {
		return _toPred[i];
	}

	public int getType() {
		return typeId;
	}

	public boolean hasPredicate() {
		return _hasPredicate;
	}

	public boolean[] hasEquality() {
		return _hasEquality;
	}

	public boolean hasEquality(int i) {
		return _hasEquality[i];
	}

	public int getPredicateType() {
		int typ = -1;

		// klarinda 20060811, add node() if (_nameTest.compareTo("*") == 0){
		if (_nameTest.compareTo("*") == 0 || _nameTest.compareTo("node()") == 0) {// klarinda
			// 20060811
			typ = 0;
		} else {
			typ = 1;
		}

		return typ;
	}

	// the following functions added by klarinda 20061122
	public boolean[] getAndExpr() {
		return _andExpr;
	}

	public boolean getAndExpr(int i) {
		return _andExpr[i];
	}

	public boolean[] getOrExpr() {
		return _orExpr;
	}

	public boolean getOrExpr(int i) {
		return _orExpr[i];
	}

	public int[] getNoOfCloseParenthesis() {
		return _noOfCloseParenthesis;
	}

	public int getNoOfCloseParenthesis(int i) {
		return _noOfCloseParenthesis[i];
	}

	public int[] getNoOfOpenParenthesis() {
		return _noOfOpenParenthesis;
	}

	public int getNoOfOpenParenthesis(int i) {
		return _noOfOpenParenthesis[i];
	}

}
